import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] 

# 数据
companies = ['泰豪科技', '华丰股份']
current_roe = [1.58, 0.61]
previous_roe = [3.3, 3.3]
increase_decrease = ['-1.72', '+0.70']

# 设置柱状图的位置
x = np.arange(len(companies))
width = 0.35

# 创建图表
fig, ax = plt.subplots()

# 绘制柱状图
bars1 = ax.bar(x - width/2, current_roe, width, label='本期发生额 (%)')
bars2 = ax.bar(x + width/2, previous_roe, width, label='上期发生额 (%)')

# 添加文本标签、标题和自定义x轴刻度标签
ax.set_ylabel('净资产收益率 (%)')
ax.set_title('净资产收益率分析')
ax.set_xticks(x)
ax.set_xticklabels(companies)
ax.legend()

# 在柱状图上添加增减幅度标签
for i, (bar1, bar2) in enumerate(zip(bars1, bars2)):
    height1 = bar1.get_height()
    height2 = bar2.get_height()
    ax.annotate(f'{increase_decrease[i]}',
                xy=(bar1.get_x() + bar1.get_width() / 2, height1),
                xytext=(0, 3),  # 3 points vertical offset
                textcoords="offset points",
                ha='center', va='bottom')
    ax.annotate(f'{increase_decrease[i]}',
                xy=(bar2.get_x() + bar2.get_width() / 2, height2),
                xytext=(0, -3),  # -3 points vertical offset
                textcoords="offset points",
                ha='center', va='top')

# 显示图表
plt.show()